<template>
	 
	
		<view class="register">
			
			<view class="content">
				<!-- 头部logo -->
				<view class="header">
					省
				</view>
				<!-- 主体 -->
				<view class="main">
					<wInput v-model="phoneData" type="number" maxlength="11" placeholder="请输入手机号"></wInput>

					<wInput v-model="verCode" type="number" maxlength="6" placeholder="请输入验证码" isShowCode ref="runCode" @setCode="sendMsg()"></wInput>
					<wInput v-if="show" v-model="invitation" type="number" isShowGet ref="setNumberCode" @setNumberCode="isShowGet()"
					 placeholder="请填写邀请码"></wInput>
				</view>
			</view>
			
			<!-- #ifdef H5-->
			<wButton text="登 录" :rotate="isRotate" @click.native="wxGetUserInfo()"></wButton>
			<!-- #endif -->
		
		<!-- #ifdef MP-WEIXIN  -->
			<button style="background: #E10A07;background-color: #E10A07;" class='bottom' type='primary' open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="wxGetUserInfo">
						登 录 2
			</button>
		<!-- #endif -->
		
		<!-- 底部信息 -->
		<view class="footer">
			<text @tap="isShowAgree" class="cuIcon" :class="showAgree?'cuIcon-radiobox':'cuIcon-round'">同意
			</text>
			<!-- 协议地址 -->
			<navigator url="/pages/member/mimi" open-type="navigate">《隐私政策》</navigator>和
			<navigator url="/pages/member/xieyi" open-type="navigate">《用户服务协议》</navigator>
		</view>
			
		<!-- #ifdef MP-WEIXIN -->
		<view v-if="isCanUse">
			<view>
				<view class='headers'>
					<image src='../../static/img/logo.png'></image>
				</view>
				<view class='content'>
					<view>申请获取以下权限</view>
					<text>获得你的公开信息(昵称，头像、地区等)</text>
				</view>

				<button style="background: #E10A07;background-color: #E10A07;" class='bottom' type='primary' open-type="getUserInfo"
				 withCredentials="true" lang="zh_CN" @getuserinfo="wxGetUserInfo">
					授权登录
				</button>
			</view>
		</view>
		<!-- #endif -->
	</view>

</template>

<script>
	var _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	console.log('isCanUse='+uni.getStorageSync('isCanUse'))
	export default {
		data() {
			return {
				SessionKey: '',
				OpenId: '',
				nickName: null,
				avatarUrl: null,
				isCanUse: uni.getStorageSync('isCanUse') || true, //默认为true
				platform: "mp",
				//logo图片 base64
				logoImage: '',
				phoneData: '', // 用户/电话
				passData: '123456', //密码
				verCode: "", //验证码
				showAgree: true, //协议是否选择
				isRotate: false, //是否加载旋转
				invitation: '',
				invitations: '',
				isEnable: '否',
				show: false,
			}
		},
		components: {
			wInput,
			wButton,
		},
		onLoad() {
			let a = this.$queue.getData("isEnable")
			if (a) {
				this.isEnable = a;
			}
			let relation = this.$queue.getData("relation");
			if (relation) {
				this.invitation = relation;
				this.show = false;
			}
			//#ifdef MP-WEIXIN
			
			if (relation) {
				this.invitation = relation;
			} else {
				this.invitation = this.$queue.getInvitation();
			}
			// this.login()
			//#endif

		},

		mounted() {
			_this = this;
		},
		methods: {
			//第一授权获取用户信息===》按钮触发
			wxGetUserInfo() {
				
				if(this.startReg() == false){
					return;
				}
				
				let _this = this;
				let nickName = '';
				let avatarUrl = '';
				//#ifdef MP-WEIXIN
				uni.getUserInfo({
					provider: 'weixin',
					success: function(infoRes) {
						nickName = infoRes.userInfo.nickName; //昵称
						avatarUrl = infoRes.userInfo.avatarUrl; //头像
						console.log('1 nickName='+nickName)
						_this.myLogin(nickName,avatarUrl)
						/*
						try {
							uni.setStorageSync('isCanUse', false); //记录是否第一次授权  false:表示不是第一次授权
							if (_this.$queue.getData("openid")) {
								_this.getWeixinInfo(nickName, avatarUrl);
							} else {
								_this.login()
							}
				
						} catch (e) {}
						*/
					},
					fail(res) {
						_this.myLogin(null,null)
						console.log('授权失败')
					}
				});
				
				//#endif
				
				//#ifdef H5
				_this.myLogin(null,null)
				//#endif
				
				 
				return;
				
				
			}, //登录
			myLogin(nickName,avatarUrl){
				//登录/注册
				this.$Request.postT("/user/login", {
					imageUrl: avatarUrl,
					nickName: nickName,
					phone : _this.phoneData
				}).then(res => {
					
					if (res.status === 0) {
						this.$queue.setData("token", res.data.uuid);
						this.$queue.setData("userId", res.data.userId);
						this.$queue.setData("mobile", _this.phoneData);
						//console.log('登录后 relation_id='+res.data.relation_id)
						let href = this.$queue.getData("href");
						if(href){
							this.$queue.remove("href");
							uni.redirectTo({url: href	});
						}else{
							uni.redirectTo({url: '/pages/member/user'	});
						}
						
					} else {
				
						uni.hideLoading();
						uni.showModal({
							showCancel: false,
							title: '登录失败',
							content: res.msg,
						});
					}
				});
			},
			getWeixinInfo(nickName, avatarUrl) {
				uni.showLoading({
					title: '登录中...'
				});
				this.$Request.postJson("/user/mp/update", {
					invitation: this.invitation,
					unionid: this.$queue.getData("unionid"),
					imageUrl: avatarUrl,
					nickName: nickName,
					openid: this.$queue.getData("openid")
				}).then(res => {
					if (res.status === 0) {
						this.$queue.setData("token", res.data.uuid);
						this.$queue.setData("userId", res.data.userId);
						this.$queue.setData("mobile", _this.phoneData);
						this.getUserInfo(res.data.userId, res.data.uuid);
					} else {

						uni.hideLoading();
						uni.showModal({
							showCancel: false,
							title: '登录失败',
							content: res.msg,
						});
					}
				});
			},
			login() {
				let _this = this;
				uni.showLoading({
					title: '登录中...'
				});
				// 1.wx获取登录用户code
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						let code = loginRes.code;

						_this.$Request.getT("/wx/mp/login?code=" + code).then(res => {
							if (res.status === 0) {
								_this.$queue.setData("openid", res.data.open_id)
								_this.$queue.setData("unionid", res.data.unionid)
								uni.hideLoading()
								//非第一次授权获取用户信息
								uni.getUserInfo({
									provider: 'weixin',
									success: function(infoRes) { //获取用户信息后向调用信息更新方法
										let nickName = infoRes.userInfo.nickName; //昵称
										let avatarUrl = infoRes.userInfo.avatarUrl; //头像
										if (nickName) {
											_this.getWeixinInfo(nickName, avatarUrl);
										}
									}
								});
							}
						});
					},
				});
			},
			//向后台更新信息
			updateUserInfo() {
				let _this = this;

			},
			navBack() {
				uni.navigateBack();
			},
			isShowGet() {
				this.show = true;
				this.invitation = this.$queue.getInvitation()
			},
			sendMsg() {
				const {
					phoneData
				} = this;
				if (!phoneData) {
					this.$queue.showToast("请输入手机号");
				} else if (phoneData.length !== 11) {
					this.$queue.showToast("请输入正确的手机号");
				} else {
					this.$queue.showLoading("正在发送验证码...");
					this.$Request.getT("/user/sendMsg/" + phoneData + "/login").then(res => {
						if (res.status === 0) {
							if (res.data === 'register') {
								this.show = true;
							}
							this.sending = true;
							this.$queue.showToast('验证码发送成功请注意查收');
							this.$refs.runCode.$emit('runCode');
							uni.hideLoading();
						} else {
							uni.hideLoading();
							uni.showModal({
								showCancel: false,
								title: '短信发送失败',
								content: res.msg ? res.msg : '请一分钟后再获取验证码',
							});
						}
					});
				}
			},
			isShowAgree() {
				//是否选择协议
				_this.showAgree = !_this.showAgree;
			},
			startReg() {
				//注册
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.showAgree == false) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '请先同意《协议》'
					});
					return false;
				}
				if (this.phoneData.length != 11) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '手机号不正确'
					});
					return false;
				}
				if (this.passData.length < 6) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不能低于六位'
					});
					return false;
				}
				if (this.verCode.length != 6) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '验证码不正确'
					});
					return false;
				}
				if (this.show) {
					if (this.invitation.length == 0) {
						uni.showToast({
							icon: 'none',
							position: 'bottom',
							title: '请输入邀请码'
						});
						return false;
					}
				}
				_this.isRotate = true;
				
				
				_this.isRotate = false;
				uni.hideLoading();
				return true;
				
				this.$Request.postJson("/user/bindOpenid", {
					pwd: _this.passData,
					phone: _this.phoneData,
					msg: _this.verCode,
					platform: _this.platform,
					invitation: _this.invitation,
					openid: this.$queue.getData("openid")
				}).then(res => {
					if (res.status === 0) {
						this.$queue.setData("token", res.data.uuid);
					
						this.$queue.setData("userId", res.data.userId);
						this.$queue.setData("mobile", _this.phoneData);
						this.getUserInfo(res.data.userId, res.data.uuid);
					} else {
						_this.isRotate = false;
						uni.hideLoading();
						uni.showModal({
							showCancel: false,
							title: '登录失败',
							content: res.msg,
						});
					}
				});

			},

			getUserInfo(userId, token) {
				this.$Request.getT("/user/" + userId).then(res => {
					if (res.status === 0) {
						this.$queue.setData("image_url", res.data.image_url ? res.data.image_url : '/static/img/common/logo.jpg');
						this.$queue.setData("relation_id", res.data.relationId);
						this.$queue.setData("relation", res.data.invitation);
						this.$queue.setData("grade", res.data.grade);
						this.$queue.setData('pddpid', res.data.pdd);
						this.$queue.setData('jdpid', res.data.jd);
						this.$queue.setData("isInvitation", res.data.isInvitation);
						this.$queue.setData("nickName", res.data.nickName ? res.data.nickName : res.data.phone);
						this.$queue.setData("gender", parseInt(res.data.gender));
						let href = this.$queue.getData("href");
						if (href) {
							if (href === '/pages/member/publisher') {
								this.$queue.remove("href");
								uni.redirectTo({
									url: '/pages/member/publisher?uid=' + userId + '&token=' + token
								});
							} else if (href === '/pages/member/user') {
								this.$queue.remove("href");
								uni.switchTab({
									url: '/pages/member/user'
								});
							} else {
								this.$queue.remove("href");
								uni.redirectTo({
									url: href
								});
							}
						} else {
							uni.switchTab({
								url: '/pages/index/index'
							});
						}

					} else {
						uni.showModal({
							showCancel: false,
							title: '登录失败',
							content: res.msg,
						});
						this.$queue.logout();
					}
					_this.isRotate = false
				});
			}
		}
	}
</script>

<style lang='scss'>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");

	.back-btn {
		position: absolute;
		left: 20px;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 20px;
		font-size: 20px;
		opacity: 0.8;
		color: $font-color-dark;
	}

	.headers {

		margin: 90rpx 0 90rpx 50rpx;
		border-bottom: 1px solid #ccc;
		text-align: center;
		width: 650rpx;
		height: 300rpx;
		line-height: 450rpx;
	}

	.headers image {
		border-radius: 100upx;
		width: 200rpx;
		height: 200rpx;
	}

	.content {
		margin-left: 50rpx;
		margin-bottom: 90rpx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 40rpx;
	}

	.bottom {
		line-height: 80upx;
		border-radius: 80upx;
		margin: 70rpx 50rpx;
		height: 80upx;
		font-size: 35rpx;
	}
</style>
